<script setup>
/*
  aticle-item 组件  接收父级(forum)传来的 item 展示单篇文章
*/
import { useForumStore } from "@/store/forum.js";
import { storeToRefs } from "pinia";
import { useUserStore } from "@/store/user.js";
import { apiView } from "@/api/forum.js";
import useUserAction from "@/hooks/useUserAction";
// 用户行为
const { handleAction } = useUserAction()
const userStore = useUserStore();
const { userInfo, token } = storeToRefs(userStore);
const forumStore = useForumStore();
const { currentArticle } = storeToRefs(forumStore);
defineProps({
  item: Object,
});
// 查看某一篇文章  存入pinia - 以供 articleDetail 页面使用
const changeArticle = async (item) => {
  currentArticle.value = item;
  uni.navigateTo({
    url: "/pages/articleDetail/articleDetail",
  });
  // 如果已登录 那就发浏览记录请求 + 存入pinia
  if (token.value) {
    const res = await apiView({ userId: userInfo.value.id, articleId: item.id });
    // res : 请求该用户最新的浏览历史记录(与后端数据同步)
    // console.log("apiView", res);
    userInfo.value.history = res
    userInfo.value.history = [...new Set(userInfo.value.history)] // 去重
  }
};
</script>

<template>
  <view class="item">
    <!-- 作者信息 -->
    <view class="info">
      <view class="author">
        <image :src="item.authorImage" mode="aspectFill" />
        <text>{{ item.authorName }}</text>
      </view>
      <uni-dateformat
        :date="item.createTime"
        format="yyyy-MM-dd"
      ></uni-dateformat>
    </view>
    <!-- 文字叙述 + 文章封面 -->
    <view
      class="title-box"
      @click="changeArticle(item)"
      :style="
        item.coverImage
          ? 'grid-template-columns: 1fr 150rpx;'
          : 'grid-template-columns: 1fr;'
      "
    >
      <view class="left">
        <!-- 标题 -->
        <h1 class="forum-title1">{{ item.title1 }}</h1>
        <!-- 富文本概述 -->
        <view class="forum-title2">
          <mp-html :content="item.previewContent" />
        </view>
      </view>
      <view class="right" v-if="item.coverImage">
        <!-- 封面图片 -->
        <image :src="item.coverImage" mode="aspectFill" />
      </view>
    </view>
    <!-- 分类盒子 -->
    <view class="sortBox">
      <view class="sort" v-if="item.sortId1"
        ><span
          style="
            font-weight: 700;
            color: #fc84a6;
            font-size: 20rpx;
            margin-right: 3px;
          "
          >#</span
        >{{ item.sortName1 }}
      </view>
    </view>
    <!-- 操作盒子 -->
    <view class="actionBox">
      <!-- 点赞 -->
      <!-- hand-up-filled -->
      <view
        class="action-item action-like"
        @click="handleAction({ actionType: 1, postId: item.id })"
        ><uni-icons
          :type="item.actionType1 ? 'hand-up-filled' : 'hand-up'"
          size="20"
          :color="item.actionType1 ? '#FC84A6' : '#000'"
        ></uni-icons>
        <!-- 始终显示text（0也显示，避免DOM结构变化），仅隐藏0值 -->
        <text
          :style="{
            color: item.actionType1 ? '#FC84A6' : '#000',
            visibility: item.upvote > 0 ? 'visible' : 'hidden',
          }"
        >
          {{ item.upvote || 0 }}
        </text>
      </view>
      <!-- 踩 -->
      <view @click="handleAction({ actionType: 3, postId: item.id })">
        <uni-icons
          :type="item.actionType3 ? 'hand-down-filled' : 'hand-down'"
          size="20"
        ></uni-icons>
      </view>
      <!-- 收藏 -->
      <view
        class="action-item"
        @click="handleAction({ actionType: 2, postId: item.id })"
        ><uni-icons
          size="20"
          :color="item.actionType2 ? '#FECD10' : '#000'"
          :type="item.actionType2 ? 'star-filled' : 'star'"
        ></uni-icons>
      </view>
      <!-- 转发 -->
      <view
        class="action-item"
        @click="handleAction({ actionType: 10086, id: item.id })"
        ><uni-icons type="redo" size="20"></uni-icons
      ></view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.item {
  background: #fff;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  padding: 28rpx 32rpx;
  display: flex;
  flex-direction: column;
  transition: all 0.25s ease;

  &:active {
    transform: scale(0.98);
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  }

  .title-box {
    display: grid;
    column-gap: 20rpx;
    align-items: center;
    margin: 15rpx 0 10rpx 0;

    .left {
      min-width: 0;
      display: flex;
      flex-direction: column;
      gap: 10rpx;

      .forum-title1 {
        font-size: 32rpx;
        color: #1a1a1a;
        font-weight: 600;
        line-height: 1.4;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        text-overflow: ellipsis;
        word-break: break-all;
        overflow: hidden;
        width: 100%;
      }

      .forum-title2 {
        color: #666;
        font-size: 26rpx;
        line-height: 1.5;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        height: 75rpx;
        width: 100%;

        /* 保证 mp-html 内容不会撑开 */
        mp-html,
        p,
        span,
        strong {
          max-width: 100% !important;
          word-wrap: break-word;
          word-break: break-all;
          overflow: hidden;
          display: inline !important;
        }
      }
    }

    .right {
      text-align: right;

      image {
        width: 150rpx;
        height: 150rpx;
        border-radius: 8rpx;
      }
    }
  }

  /* 可选优化：让 mp-html 内容继承样式，不放大 */
  .title2 rich-text,
  .title2 p,
  .title2 span {
    font-size: inherit !important;
    color: inherit !important;
    line-height: inherit !important;
    display: inline !important;
  }

  .info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 24rpx;
    color: #999;

    .author {
      display: flex;
      align-items: center;
      gap: 15rpx;

      text:first-child {
        color: #9499a0;
        font-weight: 500;
      }

      image {
        width: 50rpx;
        height: 50rpx;
        border-radius: 50rpx;
      }
    }
  }

  .sortBox {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
    margin-top: 8rpx;

    .sort {
      display: flex;
      align-items: center;
      color: #666;
      font-size: 18rpx;
      background: #efefef;
      border-radius: 20rpx;
      padding: 4rpx 14rpx;
    }
  }

  .actionBox {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding-top: 10rpx;
    align-items: center;

    .action-item {
      display: flex;
      align-items: center;
      gap: 5rpx;

      /* 点赞按钮专属样式：固定宽度 */
      .action-like {
        width: 80rpx;
        /* 根据实际需求调整（图标20rpx + 最大数字3位约40rpx + gap5rpx，预留15rpx） */
        justify-content: center;
        /* 居中显示（可选，也可以用 flex-start） */
      }

      text {
        font-size: 20rpx;
        color: #999;
      }
    }
  }
}
</style>
